package com.lmscn.lms.api.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;

import java.util.Date;
import java.util.List;

/**
 * Created by Czl on 2015/9/11.
 */
@SuppressWarnings("serial")
public class LeiTaiStudent extends Model<LeiTaiStudent> {

    public static final LeiTaiStudent dao = new LeiTaiStudent();

    /**
     * 保存攻擂
     * @param leiTaiStudent
     * @param leiId
     * @param studentId
     * @param rightCount
     * @param questionCount
     * @param usedTimes
     * @param gonglei_times
     */
    public void saveLeiTai(LeiTaiStudent leiTaiStudent,Long leiId,Long studentId,String name,
                           int rightCount,int totalScore,int questionCount,int usedTimes,int gonglei_times){
        leiTaiStudent.set("leitai_id",leiId);
        leiTaiStudent.set("student_id",studentId);
        leiTaiStudent.set("student_name",name);
        leiTaiStudent.set("right_count",rightCount);
        leiTaiStudent.set("score",totalScore);
        leiTaiStudent.set("question_count",questionCount);
        leiTaiStudent.set("used_time",usedTimes);
        leiTaiStudent.set("gonglei_times",gonglei_times);
        leiTaiStudent.set("start_time",new Date());
        leiTaiStudent.remove("id").save();
    }

    /**
     * 查询参加擂台攻擂的学员数
     * @param leiTaiId
     * @return
     */
    public Long getJoinLeiTaiCount(Long leiTaiId){
        return Db.queryLong("select count(distinct t.student_id) from t_leitai_student t " +
                "where t.leitai_id = ? ",leiTaiId);
    }

    /**
     * 查询是否参加攻擂
     * @param leiTaiId
     * @param studentId
     * @return
     */
    public LeiTaiStudent getIfJoinLeiTai(Long studentId,Long leiTaiId){
        return dao.findFirst("select t.* from t_leitai_student t " +
                "where t.leitai_id = ? and t.student_id = ? ", leiTaiId, studentId);
    }
    /**
     * 根据学员ID,擂台ID
     * 查询是否存在已有攻擂
     * @param studentId
     * @param leitaiId
     * @return
     */
    public LeiTaiStudent findByStuIdAndLeiId(Long studentId,Long leitaiId){
        return dao.findFirst("select t.* from t_leitai_student t where t.leitai_id = ? and t.student_id = ?", leitaiId, studentId);
    }

    /**
     * 查询超越了多少人
     * @param leitaiId
     * @return
     */
    public Long fightSuccessCount(Long leitaiId,int rightCount,Long studentId){
        return Db.queryLong("select count(t.student_id) from t_leitai_student t " +
                "where t.leitai_id = ? and t.right_count < ? and t.student_id != ? ", leitaiId, rightCount, studentId);
    }

    /**
     * 有多少人参与，
     * 不包含自己
     * @param leitaiId
     * @param studentId
     * @return
     */
    public Long joinLeiNotContainSelf(Long leitaiId,Long studentId){
        return Db.queryLong("select count(t.student_id) from t_leitai_student t where t.leitai_id = ? and t.student_id != ?",leitaiId,studentId);
    }

    /**
     * 查询擂台的学员
     * @param leitaiId
     * @return
     */
    public List<LeiTaiStudent> findLeiStudent(Long leitaiId,int num){
        return dao.find("select t.* from t_leitai_student t where t.leitai_id =?  order by t.right_count desc limit ?", leitaiId, num);
    }

    /**
     * 查询学员排名
     * @param leitaiId
     * @param rightCount
     * @return
     */
    public Long stuRank(Long leitaiId,int rightCount){
        return Db.queryLong("select count(t.student_id) from t_leitai_student t where t.leitai_id = ? and t.right_count > ?", leitaiId, rightCount);
    }

}
